﻿using System;
using System.IO;
using System.Net.Security;
using Deer.Log;
using Letter.Net.Tcp;

namespace Telex;

public sealed class ClientBuilder : ABuilder<ClientBuilder, IRpcClient>
{
    private SslClientConfig sslClientConfig;
    
    
    public ClientBuilder ConfigureSslClientOptions(SslClientOptions sslClientOptions, Func<Stream, SslStream> sslClientStreamFactory)
    {
        if (sslClientOptions == null)
        {
            throw new ArgumentNullException(nameof(sslClientOptions));
        }

        if (sslClientStreamFactory == null)
        {
            throw new ArgumentNullException(nameof(sslClientStreamFactory));
        }

        this.sslClientConfig = new SslClientConfig();
        this.sslClientConfig.sslClientOptions = sslClientOptions;
        this.sslClientConfig.sslClientStreamFactory = sslClientStreamFactory;
        
        return this;
    }

    public override IRpcClient Build()
    {
        if (logger == null)
        {
            logger = new DefaultLogger();
        }

        return new RpcClient(logger, sslClientConfig, secretkey, protocolMap, onChannelActive, onChannelInactive);
    }
}

